﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "CommandButtonMenuDefinition.h"
#include "FindByIdPredicate.h"
#include "GbmpNew.h"
#include "ControlDefinitionChangeEvent.h"
#include "GbmpFileSystem.h"
#include "FilePath.h"
#include "IMenuItemDefinition.h"
#include "CommandControlDefinition.h"
#include "ICommandMenuItemDefinition.h"
#include <algorithm>
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

CommandButtonMenuDefinition::CommandButtonMenuDefinition(const std::wstring& id, const std::wstring& commandId, const std::wstring& caption, const std::wstring& iconPath, const std::wstring& tooltip)
    : m_basicControlDefinition(NEW_AS_OWNER_PTR(CommandControlDefinition, id, caption, tooltip, commandId))
{
    m_isAutoChangeDisplayInformation = false;
    m_pButtonMenuDefineChangeEvent = NEW_AS_OWNER_PTR(ControlDefinitionChangeEvent, L"CommandButtonMenuDefinition", this);
    m_iconPath = iconPath == L"" ? iconPath : FilePath::GetAbsolutePath(iconPath, gcmp::FileSystem::GetExeDirPath());
}

CommandButtonMenuDefinition::~CommandButtonMenuDefinition()
{
}

std::wstring CommandButtonMenuDefinition::GetId() const
{
    DBG_WARN_AND_RETURN_UNLESS(m_basicControlDefinition, L"", L"m_basicControlDefinition为空",L"GDMPLab",L"2024-03-30");
    return m_basicControlDefinition->GetId();
}

std::wstring CommandButtonMenuDefinition::GetCaption() const
{
    DBG_WARN_AND_RETURN_UNLESS(m_basicControlDefinition, L"", L"m_basicControlDefinition为空",L"GDMPLab",L"2024-03-30");
    return m_basicControlDefinition->GetCaption();
}

std::wstring CommandButtonMenuDefinition::GetTooltip() const
{
    DBG_WARN_AND_RETURN_UNLESS(m_basicControlDefinition, L"", L"m_basicControlDefinition为空",L"GDMPLab",L"2024-03-30");
    return m_basicControlDefinition->GetTooltip();
}

std::wstring CommandButtonMenuDefinition::GetCommandId() const
{
    DBG_WARN_AND_RETURN_UNLESS(m_basicControlDefinition, L"", L"m_basicControlDefinition为空",L"GDMPLab",L"2024-03-30");
    return m_basicControlDefinition->GetCommandId();
}

std::wstring CommandButtonMenuDefinition::GetIconPath() const
{
    return m_iconPath;
}

IMenuItemDefinition* CommandButtonMenuDefinition::GetItem(const std::wstring& id) const
{
    auto search = std::find_if(m_items.begin(), m_items.end(), FindPtrBIdPredicate<OwnerPtr<IMenuItemDefinition>>(id));
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(search != m_items.end(), L"找不到菜单项",L"GDMPLab",L"2024-03-30");
    return const_cast<IMenuItemDefinition*>(search->get());
}

std::vector<IMenuItemDefinition*> CommandButtonMenuDefinition::GetAllItems() const
{
    std::vector<IMenuItemDefinition*> items;
    for (auto& item : m_items)
    {
        items.push_back(const_cast<IMenuItemDefinition*>(item.get()));
    }
    return items;
}

bool CommandButtonMenuDefinition::AddItem(OwnerPtr<IMenuItemDefinition> opMenuItemDefinition)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(opMenuItemDefinition, L"pMenuItemDefinition指针是空",L"GDMPLab",L"2024-03-30");
    
    auto iter = std::find(m_items.begin(), m_items.end(), opMenuItemDefinition);
    if (iter != m_items.end())
    {
        DBG_WARN(L"menuItem已存在",L"GDMPLab",L"2024-03-30");
        return false;
    }
    IMenuItemDefinition* pMenuItemDefinition = opMenuItemDefinition.get();
    if (ICommandMenuItemDefinition* pCmdMenuItemDef = dynamic_cast<ICommandMenuItemDefinition*>(pMenuItemDefinition))
    {
        pCmdMenuItemDef->SetParentCommandMenuDefinition(this);
    }
    m_items.emplace_back(TransferOwnership(opMenuItemDefinition));
    ControlDefinitionChangeEventArgs arguments(ControlDefinitionChangeEventType::Add);
    arguments.SetControlDefinition(pMenuItemDefinition);
    DBG_WARN_AND_RETURN_FALSE_UNLESS(m_pButtonMenuDefineChangeEvent, L"m_pButtonMenuDefineChangeEvent为空",L"GDMPLab",L"2024-03-30");
    m_pButtonMenuDefineChangeEvent->Emit(&arguments);
    return true;
}

bool CommandButtonMenuDefinition::InsertItem(const std::wstring& beforeId, OwnerPtr<IMenuItemDefinition> opMenuItemDefinition)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(opMenuItemDefinition, L"pMenuItemDefinition指针是空",L"GDMPLab",L"2024-03-30");
    auto search = std::find_if(m_items.begin(), m_items.end(), FindPtrBIdPredicate<OwnerPtr<IMenuItemDefinition>>(beforeId));
    DBG_WARN_AND_RETURN_FALSE_UNLESS(search != m_items.end(), L"没找到插入参考的Item",L"GDMPLab",L"2024-03-30");
    int index = static_cast<int>(std::distance(m_items.begin(), search));
    auto iter = std::find(m_items.begin(), m_items.end(),opMenuItemDefinition);
    if (iter != m_items.end())
    {
        DBG_WARN(L"menuItem已存在",L"GDMPLab",L"2024-03-30");
        return false;
    }
    IMenuItemDefinition* pMenuItemDefinition = opMenuItemDefinition.get();
    if (ICommandMenuItemDefinition* pCmdMenuItemDef = dynamic_cast<ICommandMenuItemDefinition*>(pMenuItemDefinition))
    {
        pCmdMenuItemDef->SetParentCommandMenuDefinition(this);
    }
    m_items.emplace(search, TransferOwnership(opMenuItemDefinition));
    ControlDefinitionChangeEventArgs arguments(ControlDefinitionChangeEventType::Insert);
    arguments.SetDeleteOrInsertControlDefinitionIndex(index);
    arguments.SetControlDefinition(pMenuItemDefinition);
    DBG_WARN_AND_RETURN_FALSE_UNLESS(m_pButtonMenuDefineChangeEvent, L"m_pButtonMenuDefineChangeEvent为空",L"GDMPLab",L"2024-03-30");
    m_pButtonMenuDefineChangeEvent->Emit(&arguments);
    return true;
}

void CommandButtonMenuDefinition::DeleteAllItems()
{
    m_items.clear();

    DBG_WARN_AND_RETURN_VOID_UNLESS(m_pButtonMenuDefineChangeEvent, L"m_basicControlDefinition为空",L"GDMPLab",L"2024-03-30");
    ControlDefinitionChangeEventArgs arguments(ControlDefinitionChangeEventType::DeleteAll);
    m_pButtonMenuDefineChangeEvent->Emit(&arguments);
}

bool gcmp::CommandButtonMenuDefinition::IsAutoChangeDisplayInformation() const
{
    return m_isAutoChangeDisplayInformation;
}

bool gcmp::CommandButtonMenuDefinition::SetAutoChangeDisplayInformation(bool isAutoChangeDisplayInformation)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(m_pButtonMenuDefineChangeEvent, L"m_pButtonMenuDefineChangeEvent为空",L"GDMPLab",L"2024-03-30");
    if (m_isAutoChangeDisplayInformation != isAutoChangeDisplayInformation)
    {
        m_isAutoChangeDisplayInformation = isAutoChangeDisplayInformation;
        ControlDefinitionChangeEventArgs arguments(ControlDefinitionChangeEventType::Other);
        arguments.SetControlDefinition(this);
        m_pButtonMenuDefineChangeEvent->Emit(&arguments);
    }
    return true;
}

bool CommandButtonMenuDefinition::SetCaption(const std::wstring& caption)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(m_basicControlDefinition, L"m_basicControlDefinition为空",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_FALSE_UNLESS(m_pButtonMenuDefineChangeEvent, L"m_pButtonMenuDefineChangeEvent为空",L"GDMPLab",L"2024-03-30");
    if (m_basicControlDefinition->GetCaption() != caption)
    {
        m_basicControlDefinition->SetCaption(caption);
    }
    ControlDefinitionChangeEventArgs arguments(ControlDefinitionChangeEventType::Other);
    arguments.SetControlDefinition(this);
    m_pButtonMenuDefineChangeEvent->Emit(&arguments);
    return true;
}

bool CommandButtonMenuDefinition::SetIconPath(const std::wstring& iconPath)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(m_pButtonMenuDefineChangeEvent, L"m_pButtonMenuDefineChangeEvent为空",L"GDMPLab",L"2024-03-30");
    if (m_iconPath != iconPath)
    {
        m_iconPath = iconPath;
    }
    ControlDefinitionChangeEventArgs arguments(ControlDefinitionChangeEventType::Other);
    arguments.SetControlDefinition(this);
    m_pButtonMenuDefineChangeEvent->Emit(&arguments);
    return true;
}

bool gcmp::CommandButtonMenuDefinition::SetTooltip(const std::wstring & tooltip)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(m_basicControlDefinition, L"m_basicControlDefinition为空",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_FALSE_UNLESS(m_pButtonMenuDefineChangeEvent, L"m_pButtonMenuDefineChangeEvent为空",L"GDMPLab",L"2024-03-30");
    if (m_basicControlDefinition->GetTooltip() != tooltip)
    {
        m_basicControlDefinition->SetTooltip(tooltip);
    }
    ControlDefinitionChangeEventArgs arguments(ControlDefinitionChangeEventType::Other);
    arguments.SetControlDefinition(this);
    m_pButtonMenuDefineChangeEvent->Emit(&arguments);
    return true;
}

bool gcmp::CommandButtonMenuDefinition::SetCommandId(const std::wstring & commandId)
{
    DBG_WARN_AND_RETURN_UNLESS(m_basicControlDefinition, L"", L"m_basicControlDefinition为空",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_FALSE_UNLESS(m_pButtonMenuDefineChangeEvent, L"m_pButtonMenuDefineChangeEvent为空",L"GDMPLab",L"2024-03-30");
    if (m_basicControlDefinition->GetCommandId() != commandId)
    {
        m_basicControlDefinition->SetCommandId(commandId);
    }
    ControlDefinitionChangeEventArgs arguments(ControlDefinitionChangeEventType::Other);
    arguments.SetControlDefinition(this);
    m_pButtonMenuDefineChangeEvent->Emit(&arguments);
    return true;
}


bool gcmp::CommandButtonMenuDefinition::IsVisible() const
{
    // CommandButtonMenu可以绑定或不绑定Command。如果绑定Command且不可见，则设置不可见；
    // 其他情况都由CommandMenuItem的可见性来决定，如果CommandMenuItem都不可见，那么CommandButtonMenu也不可见。
    DBG_WARN_AND_RETURN_FALSE_UNLESS(m_basicControlDefinition, L"m_basicControlDefinition为空",L"GDMPLab",L"2024-03-30");
    if (GetCommandId() != L"")
    {
        return m_basicControlDefinition->IsVisible();
    }
    std::vector<IMenuItemDefinition*> menuItems = GetAllItems();
    for (auto pMenuItem : menuItems)
    {
        DBG_WARN_AND_CONTINUE_UNLESS(pMenuItem, L"pControlDefinition为空",L"GDMPLab",L"2024-03-30");
        bool visible = pMenuItem->IsVisible();
        if (visible)
        {
            return true;
        }
    }
    return false;
}

bool gcmp::CommandButtonMenuDefinition::IsEnabled() const
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(m_basicControlDefinition, L"m_basicControlDefinition为空",L"GDMPLab",L"2024-03-30");
    if (GetCommandId() != L"")
    {
        return m_basicControlDefinition->IsEnabled();
    }
    std::vector<IMenuItemDefinition*> menuItems = GetAllItems();
    for (auto pMenuItem : menuItems)
    {
        DBG_WARN_AND_CONTINUE_UNLESS(pMenuItem, L"pControlDefinition为空",L"GDMPLab",L"2024-03-30");
        bool enable = pMenuItem->IsEnabled();
        if (enable)
        {
            return true;
        }
    }
    return false;
}

gcmp::IControlDefinitionChangeEvent* gcmp::CommandButtonMenuDefinition::GetControlDefinitionChangeEvent() const
{
    return const_cast<ControlDefinitionChangeEvent*>(m_pButtonMenuDefineChangeEvent.get());
}
